﻿using System;
using System.Collections.Generic;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
    public class Tbl_khachhang_DAL
    {
        public SqlConnection cn = null;
        public Tbl_khachhang_DAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public Tbl_khachhang_DAL(Tbl_doanhnghiep_DAL kh)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }

        public tbl_KhachHangCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("GET_KHACHHANG", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbl_KhachHangCollection khCollection = new tbl_KhachHangCollection();

            while (dr.Read())
            {
                Tbl_khachhang_Entities kh = new Tbl_khachhang_Entities();
                kh.STT = int.Parse(dr.GetValue(0).ToString());
                kh.TenDangNhap = dr.GetValue(1).ToString();
                kh.MatKhau = dr.GetValue(2).ToString();
                kh.Hoten = dr.GetValue(3).ToString();
                kh.DiaChi = dr.GetValue(4).ToString();
                kh.SDT = dr.GetValue(5).ToString();
                kh.Email = dr.GetValue(6).ToString();
                kh.Sodutaikhoan = float.Parse(dr.GetValue(7).ToString());
                khCollection.Add(kh);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return khCollection;
        }
        public void insert(Tbl_khachhang_Entities kh)
        {
            SqlCommand cm = new SqlCommand("INSERT_KHACHHANG", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", kh.STT);
            cm.Parameters.AddWithValue("@TenDangNhap", kh.TenDangNhap);
            cm.Parameters.AddWithValue("@MatKhau", kh.MatKhau);
            cm.Parameters.AddWithValue("@HoTen", kh.Hoten);
            cm.Parameters.AddWithValue("@Diachi", kh.DiaChi);
            cm.Parameters.AddWithValue("@Sdt", kh.SDT);
            cm.Parameters.AddWithValue("@Email", kh.Email);
            cm.Parameters.AddWithValue("@Sodutaikhoan", kh.Sodutaikhoan);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("DELETE_KHACHHANG", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", iD);
            cm.ExecuteNonQuery();
        }
        public void Update(Tbl_khachhang_Entities kh)
        {
            SqlCommand cm = new SqlCommand("UPDATE_KHACHHANG", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@STT", kh.STT);
            cm.Parameters.AddWithValue("@TenDangNhap", kh.TenDangNhap);
            cm.Parameters.AddWithValue("@MatKhau", kh.MatKhau);
            cm.Parameters.AddWithValue("@HoTen", kh.Hoten);
            cm.Parameters.AddWithValue("@Diachi", kh.DiaChi);
            cm.Parameters.AddWithValue("@Sdt", kh.SDT);
            cm.Parameters.AddWithValue("@Email", kh.Email);
            cm.Parameters.AddWithValue("@Sodutaikhoan", kh.Sodutaikhoan);
            cm.ExecuteNonQuery();
        }
    }
}
